<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-28
 * Time: 22:18
 */

namespace app\api\controller\seller;
use app\common\controller\Api;

use app\common\model\Card;
use app\common\model\Bank;
use app\common\model\WithDraw;
use app\common\model\Member;
use app\common\model\Amount as AmountModel;
class Amount extends Api
{
    protected $set_rule = [
        'bank_id|银行id' => 'require',
        'bank_subbranch|开户支行' => 'require',
        'bank_username|开户名' => 'require',
        'bank_num|银行卡号' => 'require'
    ];
    
   
    
    /**
     * 余额变动列表
     */
    public function index(){
        
        
        $pararms = paramForm();
        $sellerId = $this->getUserId();
        $seller = Member::get($sellerId);
        $filter = [
            'user_id'=>$seller->id,
        ];
        
        if(isset($pararms['action'])){
            
            switch ($pararms['action']){
                case 'plus':
                    $filter['action'] = 1;
                    break;
                case 'minus':
                    $filter['action'] = -1;
                    break;
                default:
                    //啥也不干
                    break;
            }
        }
        
        $list = AmountModel::where($filter)->order('create_time desc')->paginate(20);
        
        success($list);
        
    }
    
    
    /**
     * @return mixed
     * @throws \think\exception\DbException
     * @desc 获取提现配置
     */
    public function set()
    {
        
        $card = Card::get(['user_id' => $this->getUserId()]);
        $this->assign('card', $card);
        $this->assign('banks', Bank::all(['flag' => 1]));
        return $this->fetch();
    }
    
    /**
     * 保存提现配置
     */
    public function addSet()
    {
        $params = paramForm();
        $this->validate($params,$this->set_rule);
        
        $isHas = Card::get(['user_id'=>$this->getUserId()]);
        if($isHas){
            $this->error('该用户已经有提现信息');
        }
        
        $bank = Bank::get($params['bank_id']);
        $params['bank_name'] = $bank['name'];
        $params['user_id'] = $this->getUserId();
        $card = new Card();
        $rt = $card->allowField(true)->save($params);
        if($rt){
            $this->success('新增成功','/amount/set');
        }else{
            $this->error('新增失败');
        }
        
    
    }
    
    /**
     * 更新提现配置
     */
    public function upSet()
    {
        $params = paramForm();
        $this->validate($params,$this->set_rule);
    
        $bank = Bank::get($params['bank_id']);
        $params['bank_name'] = $bank['name'];
    
        $card = new Card();
        $rt = $card->allowField(true)->save($params,['id'=>$params['id']]);
        if($rt){
            $this->success('修改成功','/amount/set');
        }else{
            $this->error('修改失败');
        }
        
    
    }
    
}